package com.webull.information.center.carwler.task;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.webull.framework.util.UtilJson;
import com.webull.information.center.carwler.task.support.TikcerNewsProcessor;
import com.webull.information.client.model.News;
import com.webull.information.client.service.InfoNewsService;

/**
 * 新闻html文件增量更新 1、历史新闻未生成HTML文件,需补执行HTML文件 2、针对新闻，HTML文件版本更新
 * 
 * @author shimingjun
 * @date 2016年8月17日 下午9:17:31
 * @version 1.0
 * @since JDK 1.8
 */
@Component
public class News_TickerHtmlAfreshPatch_Task {
	private final Logger logger = LogManager.getLogger(getClass());
	private @Autowired InfoNewsService infoNewsService;
	private @Autowired TikcerNewsProcessor tikcerNewsProcessor;
	private final Integer pageSize = 100;

	public Long fullTickerNewsCaterpillar(Long lastId, Integer loop) {
		logger.info("FullStockFinanceNews.fullStockFinanceNewsExchangeCode start exchangeCode:{},lastId:{},loop:{}", lastId, lastId, loop);
		int loop_ = 0;
		full: do {
			if (loop != null && loop > 0 && ++loop_ > loop) {
				break full;
			}
			List<News> res = infoNewsService.queryPaging(lastId, pageSize);
			if (res == null || res.isEmpty()) {
				lastId = null;
				break full;
			}
			logger.info("FullStockFinanceNews.fullStockFinanceNewsExchangeCode stocks:" + res.size());
			lastId = res.get(res.size() - 1).getId();
			process(res);
		} while (true);

		return lastId;
	}

	private void process(List<News> newsList) {
		for (News news : newsList) {
			try {
				tikcerNewsProcessor.updateNews4S3(news);
			} catch (Throwable e) {
				logger.error("process error,news:{},error:{}", UtilJson.toMap(news), e);

			}

		}
	}
}
